
#include "para.hpp"
#include "../../xcom/xrb.hpp"

namespace rb_test_correct
{
    
    rb g_rb;
    volatile uint64_t val;

    static void write_thread()
    {
        uint64_t widx = 0;
        for(auto i = 0;i < test_cnt;++i){
            
            while(-1 == (widx = g_rb.writer_get_idx()) )
                ;

            val = widx;
            g_rb.writer_done(1);
        }
        
    }

    static void read_thread()
    {
        uint64_t ridx = 0;
        
        for(auto i = 0;i < test_cnt;++i){
            while(-1 == (ridx = g_rb.reader_get_idx()) )
                ;
            if(val != ridx)
            {
                printf("err,val=%ju\n",val);
                g_rb.print();
            }
            g_rb.reader_done(1);
        }
    }

    static int  test()
    {
        g_rb.init(1,16);

        std::thread t1(write_thread);
        if(-1 != cpu_bind1)
        {
            auto bind_ret = cpu_bind(t1.native_handle(),cpu_bind1);
            X_CHECK(bind_ret,-1);
        }

        std::thread t2(read_thread);
        if(-1 != cpu_bind2)
        {
            
            auto bind_ret2 = cpu_bind(t2.native_handle(),cpu_bind2);
            X_CHECK(bind_ret2,-1);
        }

        t1.join();
        t2.join();

        printf("rb_test_correct ok.cpu1=%d,cpu2=%d\n",
            cpu_bind1,cpu_bind2);
        return 0;
    }
}